package com.pachong.android.frameworkbase.customviews.multiviewpager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/6/27
 */
public class MultiAdapter extends FragmentStatePagerAdapter implements MultiAddable {
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private Context mContext;

    public MultiAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext=context;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }


    @Override
    public void add(Fragment fragment) {
        fragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public void add(String imageUrl) {
        Fragment fragment = ImageFragment.newInstance(imageUrl);
        fragments.add(fragment);
        notifyDataSetChanged();
    }

    @Override
    public void add(ArrayList<String> imageUrls) {
        for (String imageUrl:imageUrls){
            Fragment fragment=ImageFragment.newInstance(imageUrl);
            fragments.add(fragment);
        }
        notifyDataSetChanged();
    }

    @Override
    public void add(String[] fileUrls) {
        ArrayList<String> tempList = new ArrayList<>();
        for (String temp : fileUrls) {
            tempList.add(temp);
        }
        add(tempList);
    }

    @Override
    public void add(Uri uri) {
        String imageUrl=uri.toString();
        add(imageUrl);
    }

    @Override
    public void add(int drawableResId) {
        Drawable drawable=mContext.getResources().getDrawable(drawableResId);
        add(drawable);
    }

    @Override
    public void add(Drawable drawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bitmap = bd.getBitmap();
        add(bitmap);
    }

    @Override
    public void add(Bitmap bitmap) {
        ImageView imageView=new ImageView(mContext);
        imageView.setImageBitmap(bitmap);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        add(imageView);
    }

    @Override
    public void add(View view) {
        Fragment fragment=new ViewFragment(view);
        fragments.add(fragment);
        notifyDataSetChanged();
    }
}
